package com.bootdo.acitvity.listener.message.event.message;

import com.bootdo.acitvity.listener.message.event.component.EventComponent;
import com.bootdo.acitvity.listener.message.event.orderEvent.OrderEvent;
import lombok.Data;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Data
public class EventBus {

    private volatile Set<EventSubscriber> subscribers = new HashSet<>();

    public void registerSubscriber(EventSubscriber eventSubscriber){
        subscribers.add(eventSubscriber);
    }

    public void publishEvent(Class<? extends EventSubscriber> aClass, List<OrderEvent> orderEvents) {

        Set<EventSubscriber> tempSubscribers = subscribers;
        
        for(EventSubscriber eventSubscriber:tempSubscribers){
            Class<? extends EventSubscriber> aClass1 = eventSubscriber.getClass();
            if(aClass1 !=aClass){
                eventSubscriber.update(orderEvents);
            }
        }
    }
}
